import qs from "qs"
import $u from "@/u"

const useRouter = () => {
    return {
        switchTab({ path }) {
            $u.switchTab({ url: path })
        },
        push({ path, query }) {
            path = `${path}${query ? "?" : ""}${qs.stringify(query)}`
            console.log("router.push => ", path)
            $u.navigateTo({ url: path })
        },
        back() {
            $u.navigateBack()
        },
        reLaunch({ path, query }) {
            path = `${path}${query ? "?" : ""}${qs.stringify(query)}`
            console.log("router.reLaunch => ", path)
            $u.reLaunch({ url: path })
        }

    }
}

const useRoute = () => {
    let result = { query: {} }
    let { search = '' } = window.location
    result.query = qs.parse(search.substring(1))
    return result
}

export { useRouter, useRoute }